/**
 * 版权所有(C)，中国银联股份有限公司，2002-2013，所有权利保留。
 * 
 * 项目名：	simple-service
 * 文件名：	RoleServiceImpl.java
 * 模块说明：	
 * 修改历史：
 * 2013-10-30 - Administrator - 创建。
 */
package com.aeky.simple.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aeky.simple.dao.RoleFunctionInfoDao;
import com.aeky.simple.dao.RoleInfoDao;
import com.aeky.simple.entity.RoleFunctionInfo;
import com.aeky.simple.entity.RoleInfo;
import com.aeky.simple.service.RoleInfoService;
import com.aeky.simple.vo.page.Pager;

/**
 * @author wanghui
 * 
 */
@Service("roleInfoService")
public class RoleInfoServiceImpl implements RoleInfoService {

    @Autowired
    private RoleInfoDao roleInfoDao;

    @Autowired
    private RoleFunctionInfoDao roleFunctionInfoDao;

    public List<RoleInfo> queryAll() {
        return roleInfoDao.queryAll();
    }

    public void addRoleInfo(RoleInfo roleInfo, String funcs) {
        Long roleId = roleInfoDao.addRoleInfo(roleInfo);
        List<String> funcCodeList = Arrays.asList(funcs.split(","));
        List<RoleFunctionInfo> rfiList = new ArrayList<RoleFunctionInfo>();
        RoleFunctionInfo rfi = null;
        for (String funcCodes : funcCodeList) {
            String[] arr = funcCodes.split("-");
            if (arr.length == 2) {
                rfi = new RoleFunctionInfo();
                rfi.setSysCode(arr[0]);
                rfi.setRoleId(roleId);
                rfi.setFuncCode(arr[1]);
                rfiList.add(rfi);
            }
        }
        roleFunctionInfoDao.addRoleFunctionInfo(rfiList);
    }

    public Pager<RoleInfo> queryRoleInfoList(Pager<RoleInfo> pager, Map<String, Object> paramMap) {
        return roleInfoDao.queryRoleInfoList(pager, paramMap);
    }

    public RoleInfo queryRoleInfoByIDAndVersion(Long roleId, Integer version) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("roleId", roleId);
        paramMap.put("version", version);
        return roleInfoDao.queryRoleInfoByIDAndVersion(paramMap);
    }

    public void updateRoleInfo(RoleInfo roleInfo, String funcs) {
        roleInfoDao.updateRoleInfo(roleInfo);
        final Long roleId = roleInfo.getRoleId();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("roleId", roleId);
        roleFunctionInfoDao.deleteRoleFunctionInfoByRoleId(paramMap);
        List<String> funcCodeList = Arrays.asList(funcs.split(","));
        List<RoleFunctionInfo> rfiList = new ArrayList<RoleFunctionInfo>();
        RoleFunctionInfo rfi = null;
        for (String funcCodes : funcCodeList) {
            String[] arr = funcCodes.split("-");
            if (arr.length == 2) {
                rfi = new RoleFunctionInfo();
                rfi.setSysCode(arr[0]);
                rfi.setRoleId(roleId);
                rfi.setFuncCode(arr[1]);
                rfiList.add(rfi);
            }
        }
        roleFunctionInfoDao.addRoleFunctionInfo(rfiList);
    }

    public void deleteRoleInfo(Long roleId, Integer version) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("roleId", roleId);
        paramMap.put("version", version);
        roleFunctionInfoDao.deleteRoleFunctionInfoByRoleId(paramMap);
        roleInfoDao.deleteRoleInfo(paramMap);
    }

}
